package com.highcom.servlet;

import com.highcom.bean.AdminInfo;
import com.highcom.bean.MenuInfo;
import com.highcom.bean.MenuOne;
import com.highcom.bean.MenuTwo;
import com.highcom.dao.AdminInfoDao;
import com.highcom.dao.MenuInfoDao;
import com.highcom.dao.impl.AdminInfoDaoImpl;
import com.highcom.dao.impl.MenuInfoDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 王卓逸
 * @program TODO
 * @description TODO
 * @create 2019-10-09
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String adminName = req.getParameter("adminName");
        String password = req.getParameter("password");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        AdminInfoDao adminInfoDao = new AdminInfoDaoImpl();
        AdminInfo adminInfo = adminInfoDao.selectByAdminName(adminName);
        if (null == adminInfo || (!password.equals(adminInfo.getPassword()))) {
            out.println(0);
        } else {
            //登录成功后将adminInfo对象存入session
            session.setAttribute("adminInfo", adminInfo);
            //根据usermenu和menuinfo两个表查取用户对应的菜单
            MenuInfoDao menuInfoDao = new MenuInfoDaoImpl();
            List<MenuInfo> menuInfos = menuInfoDao.selectMenuListByUserId(adminInfo.getId());
            //将菜单列表整理成树状结构
            List<MenuOne> menuOnes = new ArrayList<>();
            for (MenuInfo menuInfo :
                    menuInfos) {
                if (0 == menuInfo.getParentId()) {
                    MenuOne menuOne = new MenuOne();
                    menuOne.setId(menuInfo.getId());
                    menuOne.setMenuName(menuInfo.getMenuName());
                    menuOnes.add(menuOne);
                } else {
                    for (MenuOne menu :
                            menuOnes) {
                        if (menu.getId().equals(menuInfo.getParentId())) {
                            MenuTwo menuTwo = new MenuTwo();
                            menuTwo.setId(menuInfo.getId());
                            menuTwo.setMenuName(menuInfo.getMenuName());
                            menuTwo.setTarget(menuInfo.getTarget());
                            menuTwo.setUrl(menuInfo.getUrl());
                            menuTwo.setParentId(menuInfo.getParentId());
                            menuTwo.setIcon(menuInfo.getIcon());
                            menu.getChildren().add(menuTwo);
                        }
                    }
                }
            }
            //将整理后的菜单存入session
            session.setAttribute("menus", menuOnes);
            out.println(1);
        }
    }
}
